package svc

import (
	"github.com/SpectatorNan/gorm-zero/gormc/config/mysql"
	"github.com/apache/rocketmq-client-go/v2"
	"github.com/zeromicro/go-zero/core/logx"
	"gorm.io/gorm"
	"mrshen_rocket_comsumer/common/xrocketmq"
	"mrshen_rocket_comsumer/internal/config"
	"mrshen_rocket_comsumer/model/region"
)

type ServiceContext struct {
	Config                  config.Config
	Db                      *gorm.DB
	PushHelloGroupConsumer  rocketmq.PushConsumer
	PushHelloGroupConsumer1 rocketmq.PushConsumer
	PushHelloGroupConsumer2 rocketmq.PushConsumer
	TRegionOpenCityModel    region.TRegionOpenCityModel
}

func NewServiceContext(c config.Config) *ServiceContext {

	Db, err := mysql.Connect(c.Mysql)

	if err != nil {
		logx.Errorf("连接mysql失败: ", err)
	}

	PushHelloGroupConsumer, _ := xrocketmq.NewPushConsumer(c.HelloGroupConsumerConf.GroupName, c.HelloGroupConsumerConf.Brokers)
	PushHelloGroupConsumer1, _ := xrocketmq.NewPushConsumer(c.HelloGroup1ConsumerConf.GroupName, c.HelloGroup1ConsumerConf.Brokers)
	PushHelloGroupConsumer2, _ := xrocketmq.NewPushConsumer(c.HelloGroup2ConsumerConf.GroupName, c.HelloGroup2ConsumerConf.Brokers)

	return &ServiceContext{
		Config:                  c,
		Db:                      Db,
		PushHelloGroupConsumer:  PushHelloGroupConsumer,
		PushHelloGroupConsumer1: PushHelloGroupConsumer1,
		PushHelloGroupConsumer2: PushHelloGroupConsumer2,
		TRegionOpenCityModel:    region.NewTRegionOpenCityModel(Db, c.Cache),
	}
}
